package com.aurora.oa.work.presenter;


import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.bean.HttpResult;
import com.aurora.oa.listener.OnRequestHttpDataListener;
import com.aurora.oa.message.bean.MessageData;
import com.aurora.oa.message.bean.MessageUploadResponse;
import com.aurora.oa.message.bean.ResponseFileID;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.utils.StringUtils;
import com.aurora.oa.work.bean.AddExpenceData;
import com.aurora.oa.work.bean.ExpenceUploadResponse;
import com.aurora.oa.work.view.AddExpenceActivity;
import com.aurora.oa.work.view.AddExpenceActivityView;
import com.google.gson.Gson;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by Administrator on 2017/03/29
*/

public class AddExpenceActivityPresenterImpl implements AddExpenceActivityPresenter{
    AddExpenceActivityView view;
    RequestHttpModel model;
    //上传成功服务器返回ID
    List<File> files;
    List<String> fileIDs;
    private AddExpenceData addExpenceData;
    public AddExpenceActivityPresenterImpl(AddExpenceActivityView view) {
        this.view = view;
        model = new RequestHttpModelImpl();
    }

    @Override
    public void sendExpence(){
        //获取要发送的数据
        addExpenceData= view.getExpenceData();
        //为空就不发送消息
        if(checkMessageNull()){
            return;
        }

        view.showProgress();
        files = view.getImageFiles();
        if(files.size()==0){
            //发送没图片的报销
            Map<String, String> params = new HashMap<>();
            params.put("expensetype", addExpenceData.expensetype==null?"":addExpenceData.expensetype);
            params.put("money", addExpenceData.money+"");
            params.put("remark", addExpenceData.remark==null?"":addExpenceData.remark);
            params.put("seller", addExpenceData.seller==null?"":addExpenceData.seller);
            params.put("approvers", addExpenceData.approvers==null?"":addExpenceData.approvers);
            params.put("attachments", addExpenceData.attachments==null?"":addExpenceData.attachments);

            model.postForm(AuroraConstants.SEND_MESSAGE_URL,params,new OnRequestHttpDataListener(){

                @Override
                public void onRequestDataSuccess(String body) {

                    Gson gson = new Gson();
                    HttpResult result = gson.fromJson(body, HttpResult.class);
                    view.hideProgress();
                    if(result!=null&&result.success){
                        view.onExpenceUploadSuccess();
                    }else{
                        view.onExpenceUploadFail(result.msg);
                    }



                }

                @Override
                public void onRequestDataFail(String message) {
                    view.hideProgress();
                    view.onExpenceUploadFail(message);
                }
            });
            return;
        }
        fileIDs = new ArrayList<>();
        for(int i = 0 ;i<files.size();i++){
            model.postFile(AuroraConstants.POST_FILE_URL, files.get(i), new OnRequestHttpDataListener() {
                @Override
                public void onRequestDataSuccess(String body) {
                    Gson gson = new Gson();
                    ResponseFileID idData = gson.fromJson(body,ResponseFileID.class);
                    if(idData!=null&&idData.isSuccess()){
                        fileIDs.add(idData.getMsg());
                        //图片上传完成
                        if(fileIDs.size()==files.size()){
                            //开始上传数据

                            addExpenceData.attachments = StringUtils.getStringFromList(fileIDs);
                            Map<String, String> params = new HashMap<>();
                            params.put("expensetype", addExpenceData.expensetype==null?"":addExpenceData.expensetype);
                            params.put("money", addExpenceData.money+"");
                            params.put("remark", addExpenceData.remark==null?"":addExpenceData.remark);
                            params.put("seller", addExpenceData.seller==null?"":addExpenceData.seller);
                            params.put("approvers", addExpenceData.approvers==null?"":addExpenceData.approvers);
                            params.put("attachments", addExpenceData.attachments==null?"":addExpenceData.attachments);

                            model.postForm(AuroraConstants.SEND_MESSAGE_URL,params,new OnRequestHttpDataListener(){

                                @Override
                                public void onRequestDataSuccess(String body) {

                                    Gson gson = new Gson();
                                    HttpResult result = gson.fromJson(body, HttpResult.class);
                                    view.hideProgress();
                                    if(result!=null&&result.success){
                                        view.onExpenceUploadSuccess();
                                    }else{
                                        view.onExpenceUploadFail(result.msg);
                                    }



                                }

                                @Override
                                public void onRequestDataFail(String message) {
                                    view.hideProgress();
                                    view.onExpenceUploadFail(message);
                                }
                            });
                        }
                    }else{
                        view.hideProgress();
                        view.onFilesUploadFail(idData.getMsg());
                    }
                }

                @Override
                public void onRequestDataFail(String message) {
                    view.hideProgress();
                    view.onFilesUploadFail(message);

                }
            });
        }

    }

    //非空校验
    private boolean checkMessageNull() {

        if(StringUtils.isNullString(addExpenceData.expensetype)){
            view.onExpenceUploadFail("类别不能为空");
            return true;
        }

        if(addExpenceData.money<0){
            view.onExpenceUploadFail("金额不能小于0");
            return true;
        }
        if(StringUtils.isNullString(addExpenceData.seller)){
            view.onExpenceUploadFail("卖方不能为空");
            return true;
        }
        return false;
    }

}